package com.studio77.guessLyric.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import android.annotation.SuppressLint;

import com.google.gson.reflect.TypeToken;
import com.studio77.guessLyric.entity.QuestionEntity;
import com.studio77.guessLyric.util.GsonHelper;
import com.studio77.guessLyric.util.ShareSettingUtil;
import com.studio77.guessLyric.util.WordMapLoader;

@SuppressLint("UseSparseArrays")
public class StageManager {
	public static StageManager instance = new StageManager();

	private StageManager() {
		Questions = new ArrayList<QuestionEntity>();
		IndexMap = new HashMap<Integer, Integer>();
	}

	private static final String LAST_STAGE_INDEX_DATA_KEY = "LAST_STAGE_INDEX_DATA_KEY";
	private static final String GOLD_COUNT_KEY = "GOLD_COUNT_KEY";
	private static final String IS_SHOWED_SHOP_KEY = "IS_SHOWED_SHOP_KEY";
	private static final String LAST_QUESTIONS_COUNT_KEY = "LAST_QUESTIONS_COUNT_KEY";
	private static final String INDEX_MAP_KEY = "INDEX_MAP_KEY";

	public List<QuestionEntity> Questions;
	public HashMap<Integer, Integer> IndexMap;

	private int lastStage;
	private int goldCount;
	private boolean isShowedShop;
	private int lastQuestionsCount;

	public int getLastStage() {
		return lastStage;
	}

	public void setLastStage(int lastStage) {
		this.lastStage = lastStage;
		ShareSettingUtil.storeIntSetting(LAST_STAGE_INDEX_DATA_KEY, lastStage);
	}

	public int getGoldCount() {
		return goldCount;
	}

	public void setGoldCount(int goldCount) {
		this.goldCount = goldCount;
		ShareSettingUtil.storeIntSetting(GOLD_COUNT_KEY, goldCount);
	}

	public boolean isShowedShop() {
		return isShowedShop;
	}

	public void setShowedShop(boolean isShowedShop) {
		this.isShowedShop = isShowedShop;
		ShareSettingUtil.storeBooleanSetting(IS_SHOWED_SHOP_KEY, isShowedShop);
	}

	public int getLastQuestionsCount() {
		return lastQuestionsCount;
	}

	public void setLastQuestionsCount(int lastQuestionsCount) {
		this.lastQuestionsCount = lastQuestionsCount;
		ShareSettingUtil.storeIntSetting(LAST_QUESTIONS_COUNT_KEY,
				lastQuestionsCount);
	}

	@SuppressWarnings("unchecked")
	public void initStageData() {
		// 载入配置文件
		WordMapLoader.loadMap();
		// 载入历史配置信息
		lastStage = ShareSettingUtil
				.getIntSetting(LAST_STAGE_INDEX_DATA_KEY, 0);
		// 启动获得60金币
		goldCount = ShareSettingUtil.getIntSetting(GOLD_COUNT_KEY, 60);
		lastQuestionsCount = ShareSettingUtil.getIntSetting(
				LAST_QUESTIONS_COUNT_KEY, 0);
		isShowedShop = ShareSettingUtil.getBooleanSetting(IS_SHOWED_SHOP_KEY,
				false);

		String indexMapStrValue = ShareSettingUtil
				.getStringSetting(INDEX_MAP_KEY);
		if (!indexMapStrValue.equalsIgnoreCase("null")) {
			IndexMap = (HashMap<Integer, Integer>) GsonHelper
					.getObjectbyJsonString(indexMapStrValue,
							new TypeToken<HashMap<Integer, Integer>>() {
							}.getType());
			if (Questions.size() > lastQuestionsCount) {
				// 有新关卡加入，重新排列新关卡，并加入到IndexMap中
				Random r = new Random((new Date()).getTime());
				List<Integer> templst = new ArrayList<Integer>();
				for (int i = lastQuestionsCount; i < Questions.size(); i++) {
					templst.add(i);
				}
				for (int i = 0; i < (Questions.size() - lastQuestionsCount); i++) {
					int x = r.nextInt(Questions.size() - lastQuestionsCount);
					int temp = templst.get(i);
					templst.set(i, templst.get(x));
					templst.set(x, temp);
				}
				for (int i = 0; i < Questions.size() - lastQuestionsCount; i++) {
					IndexMap.put(i + lastQuestionsCount, templst.get(i));
				}

				// 保存IndexMap
				ShareSettingUtil.storeStringSetting(INDEX_MAP_KEY,
						GsonHelper.getJsonString(IndexMap));

				setLastQuestionsCount(Questions.size());
			}
		} else {
			Random r = new Random((new Date()).getTime());
			List<Integer> templst = new ArrayList<Integer>();
			for (int i = 0; i < Questions.size(); i++) {
				templst.add(i);
			}
			for (int i = 0; i < Questions.size(); i++) {
				int x = r.nextInt(Questions.size());
				int temp = templst.get(i);
				templst.set(i, templst.get(x));
				templst.set(x, temp);
			}
			for (int i = 0; i < Questions.size(); i++) {
				IndexMap.put(i, templst.get(i));
			}

			// 保存IndexMap
			ShareSettingUtil.storeStringSetting(INDEX_MAP_KEY,
					GsonHelper.getJsonString(IndexMap));

			setLastQuestionsCount(Questions.size());
		}
	}

	/**
	 * 获取当前关卡数据
	 * 
	 * @return
	 */
	public QuestionEntity getQuestionInfo() {
		return Questions.get(IndexMap.get(lastStage));
	}

	public boolean turnNext() {
		setLastStage(lastStage + 1);
		if (lastStage < Questions.size()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获得所有备选的文字
	 * 
	 * @return 单个字列表
	 */
	public List<String> getBeixuanDaan() {
		List<String> result = new ArrayList<String>();
		QuestionEntity ques = getQuestionInfo();

		String daan = "";
		if (ques.getType() == 1) {
			daan = ques.getSongName();
		} else if (ques.getType() == 2) {
			daan = ques.getLyricStrList()[ques.getGuessLyricIndex() - 1];
		} else if (ques.getType() == 3) {
			daan = ques.getSinger();
		}

		int index = Questions.indexOf(ques);
		index++;
		if (index > Questions.size() - 1) {
			index = 0;
		}
		Random r = new Random((new Date()).getTime());
		// 向后找，只到字符够
		String beixuan = daan;
		while (true) {
			try {
				String indexDaan = "";
				if (Questions.get(index).getType() == 1) {
					indexDaan = Questions.get(index).getSongName();
				} else if (Questions.get(index).getType() == 2) {
					indexDaan = Questions.get(index).getLyricStrList()[Questions
							.get(index).getGuessLyricIndex() - 1];
				} else if (Questions.get(index).getType() == 3) {
					indexDaan = Questions.get(index).getSinger();
				}
				beixuan += indexDaan;
				if (beixuan.length() >= 30) {
					break;
				}
			} catch (Exception ex) {
				
			} finally {
				index++;
				if (index > Questions.size() - 1) {
					index = 0;
				}
			}
		}
		for (int i = 0; i < 30; i++) {
			result.add(beixuan.substring(i, i + 1));
		}

		return result;
	}
}
